<template>
  <div class="box">
    <!-- 标题 -->
    <div class="title">
      <div class="title_fan">
        <div class="el-icon-arrow-left" @click="$router.go(-1)"></div>
      </div>
      <!-- 文字 钱包余额 -->
      <div class="title_cen">
        <div class="title_cen_title">MiniMart</div>
        <!-- 文字 钱包余额 -->
        <div class="title_cen_size">{{ $t('home.Balance') }}</div>
        <div class="title_cen_flex">
          <img src="../../../../assets/static/walletDetail/12.png" alt="">
          <div>{{ isNaN(Number(info.balance)) ? '' : Number(info.balance) }}</div>
        </div>
        <div class="title_cen_color">{{ info.machine_code }}</div>
        <div v-loading="loading" @click="open" class="title_cen_backimg">OPEN</div>
      </div>
    </div>
    <!-- 商品列表 -->
    <div class="goods_list"
         v-infinite-scroll="load">
      <div class="card_title">
        <img src="../../../../assets/static/recharge/q4.png" alt=""/>
        <div>{{ $t('Products') }}</div>
      </div>
      <!--  -->
      <div class="goods_list_info">
        <div class="goods_list_info_title">
          <div>{{ $t('Products') }}</div>
          <div>{{ $t('Product Title') }}</div>
          <div>{{ $t('商品图片') }}</div>
          <div>{{ $t('List Price') }}</div>
          <div>{{ $t('Current Price') }}</div>
        </div>
        <div v-for="(item,index) in dataList" class="infinite-list-item" :key="index">
          <div class="row-wrapper">
            {{ item.goods_code }}
          </div>
          <div class="row-wrapper">
            <span>{{ item.goods_name }}</span>
          </div>
          <div>
            <img class="goodsImg" :src="item.goods_pic" alt="商品图片"/>
          </div>
          <div class="row-wrapper price">
            <span>{{ item.goods_price }}</span>
          </div>
          <div class="row-wrapper" style="justify-content: center">
            <span>{{ Number(item.shop_price) }}</span>
          </div>
        </div>
        <div v-loading="loadingGoods"></div>
      </div>
    </div>
  </div>
</template>

<script>
import {index, goodslist} from '@/api/merbership/goods'
import {add} from '@/api/merbership/openmachine'
import {bandbank, balance} from '@/api/merbership/user'
import '@/styles/flax.css'

export default {
  watch: {
    page() {
      this.nextPage()
    }
  },
  data() {
    return {
      dataList: [],
      info: {},
      machine_code: '',
      page: 1,
      // utmInfo: {
      //   utm_source: 'qrcode',
      //   utm_medium: 'kiosk',
      //   kioskid: 'B1001'
      // },
      scanData: {},
      loading: false,
      loadingGoods: false
    }
  },
  methods: {
    /**
     * 开门
     */
    open() {
      this.bandbank()
    },
    /**
     * 商品列表下滑翻页
     */
    load() {
      if (!this.loadingGoods) {
        this.page = this.page + 1
      }
    },
    // ----------------- axios -------------------
    /**
     * 获取钱包余额
     */
    balance() {
      const pd = {}
      balance(this.$qs.stringify(pd))
        .then(res => {
          if (res.data.code === 1) {
            this.info.balance = res.data.data.balance
          }
        })
    },
    /**
     * 判断用户是否有支付能力
     */
    bandbank() {
      const pd = {
        machine_code: this.scanData.kioskid
      }
      this.loading = true
      bandbank(this.$qs.stringify(pd))
        .then(res => {
          this.loading = false
          if (res.data.code === 1) {
            this.add()
          } else {
            let that = this
            setTimeout(() => {
              that.bandbank()
            }, 2000)
            // this.$message.error(res.data.msg)
          }
        })
    },
    /**
     * 扫码点击open按钮
     */
    add() {
      const pd = {
        machine_code: this.scanData.machine_code
      }
      add(this.$qs.stringify(pd))
        .then(res => {
          if (res.data.code === 1) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
        })
    },
    /**
     * 商品列表
     */
    index() {
      const pd = {}
      index(this.$qs.stringify(pd))
        .then(res => {
          this.info = res.data.data
        })
    },
    /**
     * 机器内的商品列表
     */
    goodslist() {
      this.scanData.machine_code = this.scanData.kioskid
      this.scanData['page'] = this.page
      goodslist(this.$qs.stringify(this.scanData))
        .then(res => {
          if (res.data.code === 1) {
            // this.info = res.data.data
            // this.info.machine_code = res.data.data.name
            this.dataList = res.data.data.goods_list
          }
        })
    },
    /**
     * 下一页
     */
    nextPage() {
      let pd = this.scanData
      pd['page'] = this.page
      this.loadingGoods = true
      goodslist(this.$qs.stringify(pd))
        .then(res => {
          if (res.data.code === 1) {
            this.loadingGoods = false
            if (res.data.data.goods_list.length > 0) {
              this.dataList = this.dataList.concat(res.data.data.goods_list)
            } else {
              this.$message.info('没有更多商品了')
            }
          } else {
            this.nextPage()
          }
        })
    }
  },
  activated() {
    this.scanData = this.$qs.parse(this.$route.query.data)
    this.info.machine_code = this.scanData.kioskid
    this.goodslist()
    this.balance()
  }
}
</script>

<style lang="scss" scoped>
.price {
  justify-content: center;
}

.goodsImg {
  width: 40px;
  height: 80px;
}

.box {
  //height: 90vh;
  //overflow: scroll;
  /* 标题 */
  .title {
    background-image: linear-gradient(to bottom, #7ac91b 65%, #f5f5f5 0);
    font-weight: bold;

    .title_fan {
      color: #fff;
      font-size: 18px;
      padding: 15px;
    }

    .title_cen {
      font-weight: normal;
      margin: 0 15px;
      padding: 20px 15px 6px 15px;
      background-color: #fff;
      border-radius: 15px;
      box-shadow: 0 0 10px #ccc;
      text-align: center;
      color: #333333;

      .title_cen_title {
        font-size: 25px;
        color: #67C022;
        //font-style: AlibabaPuHuiTiB;
        font-weight: bold;
        text-shadow: #67C022 0 2px 3px;
      }

      .title_cen_size {
        font-size: 11px;
        margin: 30px 0 20px 0;
      }

      .title_cen_flex {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 21.5px;
        font-weight: bold;

        img {
          width: 21px;
          height: 21px;
          margin-right: 6px;
        }
      }

      .title_cen_color {
        color: #FFB400;
        font-size: 11px;
        margin-top: 28px;
      }

      .title_cen_backimg {
        width: 180px;
        margin: auto;
        padding: 14px 0 21px 0;
        text-align: center;
        color: #FFFFFF;
        font-weight: bold;
        font-size: 12px;
        background-image: url(../../../../assets/static/scanOpen/7.png);
        background-repeat: no-repeat;
        background-size: 100% 100%;
      }
    }
  }

  /* 商品列表 */
  .goods_list {
    margin: 15px;

    .card_title {
      display: flex;
      font-size: 15px;
      font-weight: bold;

      > div {
        margin-left: 13px;
      }
    }

    .goods_list_info {
      margin: 15px 0 30px 0;
      background-color: #fff;
      border-radius: 10px;
      font-size: 11px;
      color: #666666;
      box-shadow: 0 0 10px #ccc;

      > div {
        display: flex;
        border-bottom: #D8D8D8 solid 1px;

        > div {
          width: 25%;
          padding: 14px 0;
          text-align: center;
          word-wrap: break-word;
          justify-content: center;
          word-break: break-all;
          border-right: #D8D8D8 solid 1px;
        }

        > div:last-child {
          border: none;
        }
      }

      > div:last-child {
        border: none;
      }

      .goods_list_info_title {
        font-size: 12px;
        font-weight: bold;
        color: #333333;
      }
    }
  }
}
</style>
